package leetcode.offer_two_special.hash;

import java.util.Arrays;
import java.util.HashMap;

/**
 * @author mazouri
 * @create 2021-09-09 21:35
 */
public class _32_isAnagram {
    public boolean isAnagram1(String s, String t) {
        if (s.length() != t.length() || s.equals(t)) return false;

        int[] counts = new int[26];
        for (char c : s.toCharArray()) {
            counts[c - 'a']++;
        }

        for (char c : t.toCharArray()) {
            if (counts[c - 'a'] == 0) return false;

            counts[c - 'a']--;
        }

        return true;
    }

    public boolean isAnagram(String s, String t) {
        if (s.length() != t.length() || s.equals(t)) return false;

        HashMap<Character, Integer> map = new HashMap<>();
        for (char c : s.toCharArray()) {
            map.put(c, map.getOrDefault(c, 0) + 1);
        }

        for (char c : t.toCharArray()) {
            if (!map.containsKey(c) || map.get(c) == 0) return false;

            map.put(c, map.get(c) - 1);
        }

        return true;
    }
}
